<?
function uploadMeetingData() {
	$errorcodes = array(
			UPLOAD_ERR_INI_SIZE => 'The uploaded file exceeds the upload_max_filesize directive in php.ini.',
			UPLOAD_ERR_FORM_SIZE => 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.',
			UPLOAD_ERR_PARTIAL => 'The uploaded file was only partially uploaded.',
			UPLOAD_ERR_NO_FILE => 'No file was uploaded.',
			UPLOAD_ERR_NO_TMP_DIR => 'Missing a temporary folder.',
			UPLOAD_ERR_CANT_WRITE => 'Failed to write file to disk.',
			UPLOAD_ERR_EXTENSION => 'File upload stopped by extension.',
	);

	$error = $_FILES['xlsdata']['error'];

	$_FILES['xlsdata']['error'];
	$target_path = "bin/xlsdata/";
	$target_path = $target_path . basename( $_FILES['xlsdata']['name']);
	if(move_uploaded_file($_FILES['xlsdata']['tmp_name'], $target_path)) {
		$result = "The file ".  basename( $_FILES['xlsdata']['name']).  " has been uploaded.";
	} else{
		$result = "<b>ERROR ($error):</b><br> $errorcodes[$error]";
	}

	return $result;
}

function parseMeetingData(){
	chdir('bin');
	$perlcmd = Props::get("exec", "perl");
	exec($perlcmd . ' ./MeetingDataParser.pl val', $output, $return);
	
	if ($return != 0) {
		$result = "<font color=\"red\">ERROR: DO NOT PROCEED WITH UPDATE!</font>\n";
		$result .= "An error was encountered during processing.  Please contact the developer before\nproceeding.\n\n";
	} else {
		$result = "The meeting_data.xls file has been validated.  Please resolve any format errors that\nfollow by editing the spreadsheet and performing all three steps again.\n\n";
	}

	foreach ($output as $line) {
		echo $line."\n";
		$result .= $line."\n";
	}

	chdir('..');
	return $result;
}

function loadMeetingData(){
	$result = "";

	chdir('bin');
	$perlcmd = Props::get("exec", "perl");
	exec($perlcmd . ' ./MeetingDataParser.pl sql', $output, $return);
	
	if ($return != 0) {
		$result .= "<font color=\"red\">ERROR: DO NOT PROCEED WITH UPDATE!</font>\n";
		$result .= "An error was encountered during processing.  Please contact the developer before\nproceeding.\n\n";
	} else {
		$result .= "The meeting_data.xls file has been loaded.  Please resolve any format errors that\nfollow by editing the spreadsheet and performing all three steps again.\n\n";
	}

	foreach ($output as $line) {
		$result .= $line."\n";
	}

	chdir('..');
	return $result;
}

function fileChecks(){
	if (file_exists('bin/xlsdata/meeting_data.xls')) {
		echo "<img src=\"../images/checkmark_green.png\" width=20 height=18>";
		echo "meeting_data.xls is ready. ";
		echo "The last upload was ". date("M d, Y h:m:s T",filectime('bin/xlsdata/meeting_data.xls')) .".<br><br>\n";
	} else {
		echo "<img src=\"../images/x_red.png\" width=20 height=18>";
		echo "meeting_data.xls has not been uploaded.<br><br>\n";
	}
}

function dbChecks(){
	$db = new DB();
	$result = $db->setQuery("select count(*) from meetings");
	$count = $result->fetch_row();

	if ($count[0] > 0) {
		$result = $db->setQuery("select max(updated) from meetings");
		$updated = $result->fetch_row();
		echo "<img src=\"../images/checkmark_green.png\" width=20 height=18>";
		echo "There are $count[0] meeting records in the meeting database.";
		echo "The last update was at ". date("M d, Y h:m:s T", strtotime($updated[0])) ."<br><br>\n";
	} else {
		echo "<img src=\"../images/x_red.png\" width=20 height=18>";
		echo "There are no meeting records in the meeting database.<br><br>\n";
	}
}

?>
